package com.atguigu.crowd.mvc.handler;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.atguigu.crowd.common.CrowdConstant;
import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.mvc.config.RemoveSelfException;
import com.atguigu.crowd.service.api.AdminService;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializers.Default;
import com.github.pagehelper.PageInfo;


@Controller
public class AdminHandler {
	
	@Autowired
	private AdminService adminService;
	
	@RequestMapping("/admin/update.html")
	public String update(
			Admin admin, 
			@RequestParam("pageNum") Integer pageNum, 
			@RequestParam("keyword") String keyword
			) {
		
		adminService.update(admin);
		
		return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
	}
	
	@RequestMapping("admin/to/edit/page.html")
	public String toEditPage(
			@RequestParam("adminId") Integer adminId,
			ModelMap modelMap
			) {
		
		Admin admin = adminService.getAdminById(adminId);
//		
//		System.out.println(admin);
		
		modelMap.addAttribute("admin",admin);
		
		return "admin-edit" ;
	}
	
	
	@RequestMapping("/admin/save.html")
	public String save(Admin admin) {
		
		adminService.saveAdmin(admin);
		
		return "redirect:/admin/get/page.html?pageNum"+(Integer.MAX_VALUE); 
	}
	
	@RequestMapping("/admin/remove/{adminId}/{pageNum}/{keyword}.html")
	public String remove(@PathVariable("adminId") Integer adminId,
			@PathVariable("pageNum") Integer pageNum,
			@PathVariable("keyword") String keyword,
			HttpSession session
			) {
		
		// 查出当前登录的admin
		Admin admin = (Admin)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
		
		// 看看是否是当前登录的本人
		if(admin.getId() == adminId) {
			throw new RemoveSelfException("哥们！想开点，可以先把遗产留给我嘛！");
		}
		
		// 执行删除
		adminService.remove(adminId);
		
		// 页面跳转；回到分页的页面
		
		// 尝试方案一：直接转发admin-page会无法先是分页数据
		// return "admin-page";
		
		// 尝试方案二：转发到：/admin/get/page.html，一旦刷新页面会重复执行删除浪费性能
		// return "forward:/admin/get/page.html";
		
		// 尝试方案三：重定向：/admin/get/page.html，
		// 同时为了保持原本所在的页面和查询关键词附加pageNum和keyword两个请求参数
		return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
	}
	
	@RequestMapping("/admin/get/page.html")
	public String getPageInfo(
			// 使用@RequestParam注解的defaultValue，指定默认值，在请求中没有携带对应的参数的默认值
			@RequestParam(value = "keyword", defaultValue = "") String keyword,
			
			// pageNum默认使用1
			@RequestParam(value = "pageNum", defaultValue="1") Integer pageNum,
			
			// pageSize默认使用5
			@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
			ModelMap modelMap
			
			) {
		
		// 调用Service 方法获取PageInfo对象
		PageInfo<Admin> pageInfo =  adminService.getPageInfo(keyword, pageNum, pageSize);
		
		// 将pageInfo放入到模型
		modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
		
		return "admin-page";
	}
	
	@RequestMapping("/admin/do/logout.html")
	public String doLogout(HttpSession session) {
		
		//强制session 失效
		session.invalidate();
		return "redirect:/admin/to/login/page.html";
	}
	
	@RequestMapping("/admin/do/login.html")
	public String doLogin(
			@RequestParam("loginAcct") String loginAcct,
			@RequestParam("userPswd") String userPswd,
			HttpSession session
			) {
		
		// 调用service方法执行登录检查
		// 这个方法如果能够返回admin对象说明登录成功，如果账号密码不对则会抛出异常
		Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);
		
		// 将登录成功返回的admin对象
		session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);
		
		return "redirect:/admin/to/main/page.html";
	}

}
